package com.luoran.system.service.impl;

import com.google.protobuf.ServiceException;
import com.luoran.common.core.utils.StringUtils;
import com.luoran.system.domain.MyTest;
import com.luoran.system.domain.SysCar;
import com.luoran.system.domain.dto.EchartsEntity;
import com.luoran.system.mapper.SysCarMapper;
import com.luoran.system.service.ISysCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class SysCarServiceImpl implements ISysCarService {
    @Autowired
    SysCarMapper sysCarMapper;

    @Override
    public List<SysCar> selectSysCarList(SysCar sysCar) {
        return sysCarMapper.selectSysCarList(sysCar);
    }

    @Override
    public boolean addCar(SysCar sysCar) {
        return sysCarMapper.addCar(sysCar);
    }

    @Override
    public int deleteLogininforByIds(Long infoIds) {
        return 0;
    }

    @Override
    public String importLogistics(List<SysCar> trList, boolean updateSupport, String operName) throws ServiceException {
        if(StringUtils.isNull(trList) || trList.size() == 0){
            throw new ServiceException("导入用户数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for(SysCar sysCar : trList){
            try {
                this.save(sysCar);
                successMsg.append("导入成功");
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureMsg + "导入失败:";
                failureMsg.append(msg+e.getMessage());
                //log.error(msg,e);
            }
        }
        if(failureNum > 0){
            failureMsg.insert(0,"很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }else {
            successMsg.insert(0,"恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public List<EchartsEntity> selectTypeBrand() {
        return sysCarMapper.selectTypeBrand();
    }

    private void save(SysCar sysCar) {
        sysCarMapper.addCar(sysCar);
    }


}
